#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 1999 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"%Z%%M%	%I%	%E% SMI"
#
export IFS PATH
IFS=" 	
"
PATH="/usr/bin"

# sys: system; user: login name;  cdir: current directory;
# tdir: temporary directory; pu: PUBDIR/receive/user;
cdir=`pwd`
dir=""
abs=""
sys=""
var=""
varto=""
varfrom=""
trap "exit 1" 1 2 13 15

# mktmpdir - Create a private (mode 0700) temporary directory inside of /tmp
# for this process's temporary files.  We set up a trap to remove the 
# directory on exit (trap 0), and also on SIGHUP, SIGINT, SIGQUIT, and
# SIGTERM.
#
mktmpdir() {
        tmpdir=/tmp/bnu.$$
        trap '/usr/bin/rm -rf $tmpdir' 0 1 2 3 15
        /usr/bin/mkdir -m 700 $tmpdir || exit 1
}

mktmpdir

# get options
while getopts s: FLAG; do
	case $FLAG in
	s)	sys=$OPTARG
		;;
	?)	gettext "Usage: uupick [-s sysname]\n" 1>&2;
		exit 1
		;;
	esac
done
shift `expr $OPTIND - 1`

if [ $# -gt 0 ]; then
	gettext "Usage: uupick [-s sysname]\n" 1>&2;
fi

user=`id | sed -n "/^uid=[0-9]*(\([^)]*\)).*/s//\1/p"`

if test -z "$user"
then gettext "User id required!\n" >&2; exit 1
fi

pu=/var/spool/uucppublic/receive/$user
if test -d $pu -a -s $pu
then
    for i in `/usr/bin/ls $pu`
    do
	if test $sys
	then
	    if test $sys != $i;  then continue;  fi
	fi
	if test -d $pu/$i -a -s $pu/$i
	then
	    cd $pu/$i
	    for j in `/usr/bin/ls -a`
	    do
		if test $j = "." -o $j = ".."; then continue; fi
		if test -d $j
		then printf "`gettext 'from system %s: directory %s '`" $i $j
		else printf "`gettext 'from system %s: file %s '`" $i $j
		fi
		while true
		do
		    echo '? \c'
		    if read cmd dir
		    then
			trap ": ;;" 1
			case $cmd in
			d)
			    rm -fr $j ; break ;;
			"")
			    break ;;
# options m, a:
#	If dir path begins with a slash, use full path for destination;
#	otherwise, use path relative to current dir;
#	default destination is current dir
#
#	As files are transferred, put their names in $tmpdir/$$uupick.
#	Only remove those named files from...receive/..dir if cmp
#	verifies transfer took place. then find & remove directories
#	(separate find is necessary because cpio -v doesn't print dir names)
			a|m)
			    eval dir="$dir"
			    if test $dir
			    then abs=`expr "$dir" : '/.*'`
				if test $abs != 0
				then tdir=$dir
				else tdir=$cdir/$dir
				fi
			    else
				tdir=$cdir
			    fi
			    if [ ! -d $tdir -o ! -w $tdir ]; then
				printf "`gettext 'directory %s doesn't exist or isn't writable'`" $tdir >&2
				continue
			    fi
			    if [ "$cmd" = "a" ]
			    then
				find . -depth -print | \
				grep -v '^\.$' > $tmpdir/$$uupick
				level=2
			    else
				find $j -depth -print > $tmpdir/$$uupick
				level=1
			    fi
			    cpio -pdmu $tdir < $tmpdir/$$uupick 
			    for k in `cat $tmpdir/$$uupick`
			    do
				varto="$tdir/$k"
				varfrom="$pu/$i/$k"
				if test -f $varfrom; then
				    if cmp $varfrom $varto ; then
					rm -f $varfrom
				    else
					printf "`gettext 'file %s not removed'`" $varfrom >&2
				    fi
				else
				    rmdir $varfrom 2>/dev/null
				fi
			    done
			    rm -f $tmpdir/$$uupick
			    break $level;; 
			p)
			    if test -d $j
			    then find $j -print
			    elif test -s $j 
				then cat $j
			    fi;;
			q)
			    break 3;;
			!*)
			    ex=`expr "$cmd $dir" : '!\(.*\)'`
			    tdir=`pwd`
			    cd $cdir
			    sh -c "$ex"
			    cd $tdir
			    echo '!';;
			*)
			    gettext "Usage: [d][m dir][a dir][p][q][cntl-d][!cmd][*][new-line]";;
			esac
			trap "exit 1" 1
		    else
			break 3
		    fi
		done
	    done
	fi
    done
fi
